package com.shycloud.mido.music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.common.minio.enums.BucketEnum;
import com.shycloud.common.minio.service.MinioTemplate;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.feign.RemoteMemberService;
import com.shycloud.mido.music.domain.MisMusicBanner;
import com.shycloud.mido.music.service.MisMusicBannerService;
import com.shycloud.mido.music.vo.AssociateBannerVO;
import io.swagger.annotations.ApiOperation;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/banner")
@ApiOperation(value = "首页推荐轮播图 controller")
public class BannerController {

	private MisMusicBannerService misMusicBannerService;

	private final RemoteMemberService remoteMemberService;
	/** 七牛云上传工具类 */
	private final MinioTemplate minioTemplate;

	/**
	 * 批量上传Banners， 前端需要用form传输
	 *
	 * @return
	 */
	@PostMapping("/batchUploadBanners")
	@ApiOperation("批量上传Banners")
	public R batchUploadBanners(@RequestParam("imgList") List<MultipartFile> imgList,
			@RequestParam(value = "categoryId") Integer categoryId,
			@RequestParam(value = "optId") Integer optId,
			@RequestParam(value = "name") String name) {

		if (imgList == null || imgList.size() == 0) {
			throw new BusinessException("请选择上传图片!");
		}

		List<Map<String, String>> returnList =
				misMusicBannerService.batchUploadBanners(imgList, categoryId, optId, name);

		Map checkMap = returnList.get(0);
		if (checkMap.containsKey("error")) {
			return R.builder().code(CommonConstants.FAIL).msg((String) checkMap.get("error"))
					.build();
		}
		return new R<>(returnList);
	}

	/**
	 * 关联banner图片到链接或者曲集
	 *
	 * @return
	 */
	@PostMapping("/associateBanner")
	@ApiOperation("关联banner设置")
	public R associateBanner(@RequestBody AssociateBannerVO vo) {

		String link = vo.getLink();
		//去除空格
		String linkAfter = link.replace(" ","");

		return new R<>(misMusicBannerService.associateBanner(
				linkAfter, vo.getCollectionId(), vo.getBannerId(), vo.getAssociateType()));
	}

	/**
	 * 获取所有Banners
	 *
	 * @return
	 */
	@GetMapping("/getAllBanners")
	@ApiOperation("根据 categoryId,optId 获取所有 Banners")
	public R getAllBanners(Integer current, Integer size, Integer categoryId, Integer optId) {
		return new R(misMusicBannerService.getAllBanners(current, size, categoryId, optId));
	}

	/**
	 * 获取所有Banner
	 *
	 * @return
	 */
	@GetMapping("/getAllBannersGroupByCate")
	@ApiOperation("获取所有Banner")
	public R getAllBannersGroupByCate() {
		return new R(
				misMusicBannerService.list(new QueryWrapper<MisMusicBanner>().eq("status", 0)));
	}

	/**
	 * 禁用Banner
	 *
	 * @param bannerId
	 */
	@GetMapping("/disableBanner")
	@ApiOperation("禁用Banner")
	public R disableBanner(Integer bannerId) {
		//TODO
		return new R<>(misMusicBannerService.disableBanner(bannerId));
	}

	@SneakyThrows
	@PostMapping("/uploadBanner")
	@ApiOperation("上传Banner返回URL")
	public R uploadBanner(@RequestParam("img") MultipartFile img) {

		if (img == null) {
			return new R(CommonConstants.FAIL, "请选择上传图片", false);
		}
		//提取后缀
		String fileName = img.getOriginalFilename().replaceAll(" ", "");
		//提取图片名称
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);

		String uuid = UUID.randomUUID().toString();
		//提取图片名称
		String imgName = uuid.replace("-", "") + "." + suffix;

		//上传操作指定返回域名
		String imgUrl = minioTemplate.uploadFileWX(img.getInputStream(), imgName,
				BucketEnum.ZEBRA_BANNER.getBucketName(),
				BucketEnum.ZEBRA_BANNER.getCdnUrl());

		return new R(imgUrl);
	}

	@PostMapping("/updOptBanner")
	@ApiOperation("编辑Banner")
	public R updBanner(@RequestBody MisMusicBanner banner) {
		return new R(misMusicBannerService.updateById(banner));
	}

	@GetMapping("/delOptBanner")
	@ApiOperation("删除Banner")
	public R delBanner(Integer id) {
		//获取 banner 信息
		MisMusicBanner banner = misMusicBannerService.getById(id);
		//提取 URL 信息
		String url = banner.getImgUrl();
		//执行删除操作
		misMusicBannerService.removeById(id);
		//删除七牛云资源
		return remoteMemberService.delCloudResourse(BucketEnum.ZEBRA_BANNER.getBucketName(), url);
	}


}
